//
//  NSMutableAttributedString+LLCoreText.m
//  LLFoundation
//
//  Created by my_liu on 2016/11/14.
//  Copyright © 2016年 . All rights reserved.
//

#import "NSMutableAttributedString+LLCoreText.h"

@implementation NSMutableAttributedString (LLCoreText)

/* 整个字符串范围 */
- (NSRange)range
{
    return NSMakeRange(0, self.length);
}

/* 插入图片标记 */
- (NSDictionary *)insertImageAttributedStringStyle:(LLCoreTextImageAttributedStringStyle*)imageAttributedString atIndex:(NSInteger)index
{
    if (index > self.length-1) {
        return nil;
    }
    NSAttributedString *imageAttiString = [imageAttributedString imageAttributedString];
    
    [self insertAttributedString:imageAttiString atIndex:index];
    
    NSRange imageRange = NSMakeRange(index, 1);
    NSDictionary *resultImageInfo = @{kLLCoreTextImageInfoRangeKey:[NSValue valueWithRange:imageRange],kLLCoreTextImageInfoStringKey:imageAttributedString.imageSourceString};
    
    return resultImageInfo;
}

- (NSDictionary *)replaceImageAttributedStringStyle:(LLCoreTextImageAttributedStringStyle*)imageAttributedString withImageRange:(NSRange)imageRange
{
    NSAttributedString *imageAttiString = [imageAttributedString imageAttributedString];
    
    [self insertAttributedString:imageAttiString atIndex:imageRange.location];
    NSRange emojiRange = NSMakeRange(imageRange.location, 1);
    imageRange = NSMakeRange(imageRange.location+1,imageRange.length);
    [self replaceCharactersInRange:imageRange withString:@""];
    
    NSDictionary *resultImageInfo = @{kLLCoreTextImageInfoRangeKey:[NSValue valueWithRange:emojiRange],kLLCoreTextImageInfoStringKey:imageAttributedString.imageSourceString};
    
    return resultImageInfo;
}

/* 插入多态字符串风格 */
- (void)insertAttributedStringStyle:(LLCoreTextAttributedStringStyle *)aStyle range:(NSRange)range
{
    if (!aStyle || range.location == NSNotFound) {
        return;
    }
    [self addAttributes:[aStyle attributedDictionary] range:range];
}

/* 插入关键字风格 */
- (void)setKeywordEffectByStyle:(LLCoreTextKeywordAttributedStringStyle *)aStyle
{
    if (!aStyle) {
        return;
    }
    [aStyle setKeywordEffectForString:self];
}

/* 指定范围关键字风格设定 */
- (void)setKeywordRangeEffectByStyle:(LLCoreTextKeywordAttributedStringStyle *)aStyle
{
    if (!aStyle) {
        return;
    }
    [aStyle setKeywordEffectByKeywordRangeForString:self];
}

/* 插入段落风格 */
- (void)insertParagraphStyle:(LLCoreTextParagraphStyle *)aStyle range:(NSRange)range
{
    if (!aStyle || range.location == NSNotFound) {
        return;
    }
    [self addAttributes:[aStyle paragraphAttributedDictionary] range:range];
}

@end
